﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Reflection;
using Gestion_Api.AccesoDatos;
using System.Data.SqlClient;

namespace Escuela
{
    public partial class AgregarAlumno : Form
    {

        private int nroCurso;
        private string nombre;
        private string dia;
        private string horaIni;
        private string horaFin;
        private Instituto inst;
        

        public AgregarAlumno(int idCurso, string nomCurso, string dia, string horaIni, string horaFin, Instituto inst )
        {
            InitializeComponent();
            
            this.nroCurso = idCurso;
            this.nombre = nomCurso;
            this.dia = dia;
            this.horaIni = horaIni;
            this.horaFin = horaFin;
            
            this.maskedNroCur.Text = idCurso.ToString();
            this.textNomCurso.Text = nomCurso;

            this.inst = inst;
        }

        private void AgregarAlumno_Load(object sender, EventArgs e)
        {
            try
            {
                // TODO: This line of code loads data into the 'escuelaDataSet.coop_alumno_GetAll' table. You can move, or remove it, as needed.
                this.coop_alumno_GetAllTableAdapter1.Fill(this.escuelaDataSet.coop_alumno_GetAll);

                this.cargarData2();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error cargando formulario." + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void Nuevo_Click(object sender, EventArgs e)
        {
            try
            {
                int i = this.dataGridView1.CurrentRow.Index;
                if (i >= 0)
                {
                    //verifico si es curso
                    if (this.inst.esCurso(Convert.ToInt32(this.maskedNroCur.Text.Trim())))
                    {
                        //obtengo idPago

                        //Doy de alta con el controlador
                        this.inst.inscribirAlumnoCurso(txtDNI.Text.Trim(), Convert.ToInt32(this.maskedNroCur.Text.Trim()));
                    }
                    else
                    {
                        this.inst.inscribirAlumnoSeminario(txtDNI.Text.Trim(), Convert.ToInt32(this.maskedNroCur.Text.Trim()));

                    }

                    //actualizo
                    this.cargarData2();
                }
                else
                {
                    MessageBox.Show("Debe seleccionar un alumno", "Advertencia");

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error agregando alumno. " + ex.Message, "Error", MessageBoxButtons.OK,MessageBoxIcon.Error );
            }
            

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = this.dataGridView1.CurrentRow.Index;
            if (i >= 0)
            {
                string NroAl = dataGridView1.Rows[i].Cells[0].Value.ToString();
                this.maskedNroAl.Text = NroAl.Trim();

                string dni = dataGridView1.Rows[i].Cells[1].Value.ToString();
                this.txtDNI.Text = dni.Trim();

                string nombre = dataGridView1.Rows[i].Cells[2].Value.ToString();
                this.txtNom.Text = nombre.Trim();

                string apellido = dataGridView1.Rows[i].Cells[3].Value.ToString();
                this.txtApe.Text = apellido.Trim();
            }

        }


        private void Aceptar_Click(object sender, EventArgs e)
        {
            string where = null;

            try
            {
                //Numero alumno
                if (this.maskedNroAl.Text != null && this.maskedNroAl.Text.Trim() != "")
                {
                    where = where + "idAlumno=" + this.maskedNroAl.Text.Trim() + " and ";
                }
                //dni
                if (this.txtDNI.Text != null && this.txtDNI.Text.Trim() != "")
                {
                    where = where + "dni like '%" + this.txtDNI.Text.Replace(".", "").Trim() + "%' and ";
                }
                //nombre
                if (this.txtNom.Text != null && this.txtNom.Text.Trim() != "")
                {
                    where = where + "nombre like '%" + this.txtNom.Text.Trim() + "%' and ";
                }
                //apellido
                if (this.txtApe.Text != null && this.txtApe.Text.Trim() != "")
                {
                    where = where + "apellido like '%" + this.txtApe.Text.Trim() + "%' and ";
                }

                if (where.Trim().Length > 3)
                    where = where.Substring(0, where.Length - 5);


                //armo el string del where
                //alumnosBindingSource.Filter = where;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error al buscar alumno: " + ex);
            }

        }

        private void Buscar_Click(object sender, EventArgs e)
        {
            clearTxt();
            enableTxt();
            enableBtn();
            this.Buscar.Enabled = false;
        }

        private void Cancelar_Click(object sender, EventArgs e)
        {
            clearTxt();
            disableTxt();
            disableBtn();
            //alumnosBindingSource.RemoveFilter();

        }

        private void enableTxt()
        {
            this.maskedNroAl.Enabled = true;
            this.txtDNI.Enabled = true;
            this.txtNom.Enabled = true;
            this.txtApe.Enabled = true;
        }

        private void disableTxt()
        {
            this.maskedNroAl.Enabled = false;
            this.txtDNI.Enabled = false;
            this.txtNom.Enabled = false;
            this.txtApe.Enabled = false;
        }



        private void clearTxt()
        {
            this.maskedNroAl.Clear();
            this.txtDNI.Clear();
            this.txtNom.Clear();
            this.txtApe.Clear();
        }

        private void enableBtn()
        {
            this.Aceptar.Enabled = true;
            this.Cancelar.Enabled = true;

        }

        private void disableBtn()
        {
            this.Aceptar.Enabled = false;
            this.Cancelar.Enabled = false;

        }

        private void cargarData2()
        {
            string query = null;
            
            System.Data.DataTable dt = new System.Data.DataTable();
            AccesoDB ac = new AccesoDB();
            query = "coop_cursable_GetAlumnosByCurso ";
            query += this.maskedNroCur.Text.Trim();

            SqlCommand command = new SqlCommand();
            //command.Connection = connection;
            command.CommandText = query;
            command.CommandType = CommandType.Text;

            dt = ac.execDT(command);

            //dt = ac.execDT(query);

            this.dataGridView2.DataSource = dt;

        }

        private void Editar_Click(object sender, EventArgs e)
        {
            int i = this.dataGridView2.CurrentRow.Index;
            if (i >= 0)
            {
                this.inst.bajaAlumnoCursable(txtDNI.Text.Trim(), Convert.ToInt32(this.maskedNroCur.Text.Trim()));
                
                ////verifico si es curso
                //if (this.inst.esCurso(Convert.ToInt32(this.maskedNroCur.Text.Trim())))
                //{
                //    //Doy de baja con el controlador en el curso
                //    this.inst.inscribirAlumnoCurso(txtDNI.Text.Trim(), Convert.ToInt32(this.maskedNroCur.Text.Trim()));
                //}
                //else
                //{
                //    //Doy de baja con el controlador en el seminario
                //    this.inst.bajaAlumnoSeminario(txtDNI.Text.Trim(), Convert.ToInt32(this.maskedNroCur.Text.Trim()));

                //}

                //actualizo
                this.cargarData2();
            }
            else
            {
                MessageBox.Show("El curso aun no tiene ningun alumno", "Advertencia");
            }
        }


        

        private void dataGridView2_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = this.dataGridView2.CurrentRow.Index;
            if (i >= 0)
            {
                string NroAl = dataGridView2.Rows[i].Cells[0].Value.ToString();
                this.maskedNroAl.Text = NroAl.Trim();

                string dni = dataGridView2.Rows[i].Cells[1].Value.ToString();
                this.txtDNI.Text = dni.Trim();

                string nombre = dataGridView2.Rows[i].Cells[2].Value.ToString();
                this.txtNom.Text = nombre.Trim();

                string apellido = dataGridView2.Rows[i].Cells[3].Value.ToString();
                this.txtApe.Text = apellido.Trim();
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            

        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        

        
    }
}
